import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtMultimedia 5.0
import MediaModel 1.0
ApplicationWindow {
    title: qsTr("QtAudioPlayer");
    width: 400;
    height: 600;
    visible: true;

    FileDialog{
        id: fileDialog;
        onAccepted: {
            mediaModel.importFolder(folder);
        }
    }

    menuBar: MenuBar {
        Menu {
            title: qsTr("&File")
            MenuItem {
                text: qsTr("&Open")
                onTriggered: messageDialog.show(qsTr("Open action triggered"));
            }
            MenuItem{
                text: qsTr("&Import Folder");
                onTriggered:{
                    fileDialog.selectFolder = true;
                    fileDialog.open();
                }
            }

            MenuItem {
                text: qsTr("E&xit")
                onTriggered: Qt.quit();
            }
        }
    }

    MediaModel{
        id: mediaModel;
    }
    MediaPlayer{
        id: player;
        volume: mainForm.sldVolume.value;
        onPlaying: mainForm.btnPlay.text = qsTr("Pause");
        onPaused: mainForm.btnPlay.text = qsTr("Play");
    }

    MainForm {
        id: mainForm
        anchors.fill: parent;
        sldVolume.value: 1;
        mediaList.onCurrentIndexChanged: {
            lblTitle.text = mediaModel.getName(mediaList.currentIndex);
            player.source = mediaModel.getUrl(mediaList.currentIndex);
            player.play();
        }
        btnPlay.onClicked: {
            player.playbackState==MediaPlayer.PlayingState?player.pause():player.play();
        }
        btnStop.onClicked: {
            player.stop();
        }

        sldProgress.onValueChanged: player.seek(sldProgress.value*player.duration);
    }

    MessageDialog {
        id: messageDialog
        title: qsTr("May I have your attention, please?")

        function show(caption) {
            messageDialog.text = caption;
            messageDialog.open();
        }
    }
}
